The first instance of obj whose box the line from (x1,y1) to (x2,y2) passes through, counting from (x1,y1), or nothing. The line of sight: nothing in the way means the way is clear.
Works differently on Game Boy - On the Game Boy this call looks at every object of that kind one by one and is slow when there are many (asked of a parent object, every child kind too): use it a few times a frame at most. Distances count to 1/16 of a pixel.
Works differently on Game Boy Color - On the Game Boy Color this call looks at every object of that kind one by one and is slow when there are many (asked of a parent object, every child kind too): use it a few times a frame at most. Distances count to 1/16 of a pixel.
On other consoles this differs: see Side by side and How the consoles differ.
collision_line(x1, y1, x2, y2, obj) -> ref<object>| Name | Type |
|---|---|
x1 | real |
y1 | real |
x2 | real |
y2 | real |
obj | object |
ref<object>
Draws an invisible straight line from (x1, y1) to (x2, y2) and gives back the first instance of obj whose box that line passes through - or nothing if the line goes through none. This is the line of sight function: put the things that block the view in obj, and nothing means the way is clear.
Both ends, (x1, y1) and (x2, y2), are places in the room counted from the room's top-left corner.
The line is tested against each instance's box, the same box collision_point and collision_rect use. The line hits a box if it goes through any point that collision_point would call inside that box. In practice:
A box's left and top edges count as inside it, and its right and bottom edges do not - the same rule as for a point. So a line running exactly along the left or top edge of a box hits it, and a line running exactly along the right or bottom edge misses it.
If the line goes through several instances of obj, you get the one the line reaches first, counting from (x1, y1). It is not whichever one happens to come first in the game's own list. A laser fired from a gun at (x1, y1) therefore hits the nearest thing in its way, and the order you made the instances in makes no difference. If two boxes are entered at exactly the same point, the game's own list order decides between them, and that is not the order you made them in.
If obj is a parent, the answer can be one of its children. With obj_wall as the parent of obj_door, collision_line(x1, y1, x2, y2, obj_wall) can hand back a door. The reference is typed as obj_wall, so you can use the variables and events obj_wall has through it. The nearest one along the line wins, whichever kind it is.
Two things to know, the same as for collision_rect:
nothing when there is none.instance_destroy). So "find what the line hits, destroy it, look again" never runs out of matches and never ends. To reach several, go through them with with.For a guard that should only notice the hero when nothing is in between, ask about the walls, not about the hero. Draw the line from the guard to the hero and put the walls in obj. If collision_line gives back nothing, no wall is in the way and the guard has a clear view. If it gives back a wall, the view is blocked. Asking collision_line about the hero would only tell you whether the line touches the hero, which is not the question. The first example below is exactly this.
Like every collision question, it looks at every live instance of obj. For each one it first does a quick check that rules out any box that is nowhere near the line, and only the boxes along the line's path need the exact test.
These numbers were measured by running the real engine code. The console does about 16.8 million cycles a second, so one frame is about 281,000 cycles. With 32 instances of obj alive:
| The line | One call costs about | Of a frame |
|---|---|---|
| nowhere near any of them | 10,000 cycles | 3.5% |
| straight through all of them | 16,000 cycles | 6% |
| slanting through a crowd of them | up to 32,000 cycles | up to 11% |
Each instance costs roughly half as much again as it does for collision_rect. So a few calls a frame against a few dozen walls is fine. One call from every one of a hundred instances, every frame, is not: ten calls like the middle row already use more than half a frame. Check less often (every few frames, with an alarm), use a shorter line, or put only the walls that matter in obj.
var hero = instance_nearest(x, y, obj_doc_hero);
if (hero != nothing) {
var wall = collision_line(x, y, hero.x, hero.y, obj_doc_wall);
if (wall == nothing) {
speed = 2;
direction = dir_to(x, y, hero.x, hero.y);
}
}A guard that chases the nearest hero, but only while no obj_doc_wall is between them. Walk behind a wall and the guard stops.
var target = collision_line(x, y, x + 120, y, obj_doc_enemy);
if (target != nothing) {
instance_destroy(target);
}A laser that shoots 120 pixels to the right and destroys the first enemy in its path. Because "first" means the first one the line reaches, it is always the nearest enemy that goes.
A straight line from the first point to the second, ends included, tested against each instance’s box - the same box collision_point and collision_rect use. It hits a box if the line passes through any point collision_point would call inside it. So a line that starts inside a box, ends inside one, or only clips a corner all hit; a line that stops a pixel short does not. Like collision_point, a box’s left and top edges are inside it and its right and bottom edges are not, so a line running exactly along a left or top edge hits and one running exactly along a right or bottom edge misses. A box with no size is never hit. "First" means the one the line REACHES first, walking from (x1,y1) - not whichever one the engine lists first. A laser fired from a gun therefore hits the nearest thing in its way. If two boxes are entered at the very same point, the one the engine meets first wins, and that order is not the order you made them in. If obj is a parent, a child instance can be the answer: the line is tried against the parent’s own instances and each child’s, the nearest wins (the parent’s on an exact tie), and the result is typed as obj. Two caveats, the same as collision_rect: you get the FIRST one only, never a list, and an instance destroyed earlier in this same frame is still found, because a destroyed slot is only freed at the end of the frame (see instance_destroy). For "can the enemy see the player", put the WALLS in obj, not the player: `collision_line(x, y, obj_player.x, obj_player.y, obj_wall) == nothing` means nothing is in the way. Cost: it looks at every live instance of obj, like every other collision query, and a quick check rules out each box that is nowhere near the line. Measured on the real engine code with 32 instances of obj alive: about 10,000 cycles when the line is nowhere near them, about 16,000 when it passes through all of them, and up to about 32,000 for a slanted line through a crowd - 3 to 11 percent of a frame, which is about 281,000 cycles. Each instance costs roughly half as much again as it does for collision_point or collision_rect. A few calls a frame are fine; one call from every instance, every frame, is not.